package com.glodon.paas.document.web.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.document.service.util.UserProvider;
import com.glodon.paas.document.service.util.WebService;
import com.glodon.paas.document.web.util.SecurityUtils;

@Component("userProvider")
public class DocumentUserProvider implements UserProvider {

    
    @Autowired
    private WebService webService;
    
    // TODO 后期可以考虑用缓存实现
    private static Map<String, User> userMap = new HashMap<String, User>();

    @Override
    public User getUser(String userId) {
        User u = null;
        try {
            if (SecurityUtils.getCurrentUser().getId().equals(userId)) {
                u = SecurityUtils.getCurrentUser();
            }
        } catch (Exception e) {
        }
        if (u == null) {

            if (!userMap.containsKey(userId)) {
                u = webService.getUser(userId);
                userMap.put(userId, u);
            } else {
                u = userMap.get(userId);
            }
        }
        return u;
    }

    @Override
    public User currentUser() {
        return SecurityUtils.getCurrentUser();
    }
}
